# file:Django_learning/view.py
import os
from django.http import HttpResponse
from django.shortcuts import render
from . import external_function


def page(request,n,op,m):
    if op=="add":
        html = '<h1>加法计算结果为%s</h1>'%(n+m)
    elif op=="subtract":
        html = '<h1>减法计算结果为%s</h1>' %(n-m)
    else:
        html = '<h1>出错了</h1>'
    return HttpResponse(html)




def usually(request,**kwargs):
    name=str(kwargs["name"])
    age=str(kwargs["age"])
    return HttpResponse("名字是%s年龄是%s"%(name,age))

def main_page(request,content):
    if content=="main":
        html='''<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>首页</title>
</head>
<body>
<div id="add">
    <div>累加计算器</div>
    <form action="/sum" method="get">
        <div><label>start<input type="text" name="start"></label></div>
        <div><label>step<input type="text" name="step"></label></div>
        <div><label>stop<input type="text" name="stop"></label></div>
        <label><input type="submit" value="提交"></label>
    </form>
</div>
</body>
</html>'''
        return HttpResponse(html)

def my_page(request):
    if request.method =='GET':
        # 1. request.GET["key值"] 返回key值对应的value值
        # 2. request.GET.get("key值","value") 查询不到参数的value时,返回默认的value值
        # 3. request.GET.getlist("key值") 返回key值对应的value列表
        # request.GET 得到的是<QueryDict: {'id': ['333', '111'], 'name': ['weijj']}> <class 'django.http.request.QueryDict'>
        return HttpResponse("<h1>Hello</h1>")
    else:
        return HttpResponse("该请求不是get请求")

def sum_number(request):
    if request.method =='GET':
        try:
            start=request.GET.get('start',"0")
            if not start:
                start=0
            stop=request.GET.get('stop',"101")
            step=request.GET.get('step')
            res=0
            for item in range(int(start),int(stop),int(step)):
                res+=item
            return HttpResponse("累加结果为"+str(res))
        except Exception as e:
            return HttpResponse("查询字符串无效")

def login_in(request):
    if request.method == 'GET':
        return HttpResponse(external_function.login_html)
    elif request.method == 'POST':
        username=request.POST.get('username',"属性错误")
        return HttpResponse("用户名是:%s" % username)

# 以下为MVT模式下构建的博客网站
def index(request):
    return render(request, 'blok/min.html')

def article(request):
    return render(request, 'blok/article.html')

def photo(request):
    return render(request, 'blok/myphoto.html')

def register(request):
    return render(request, 'blok/register.html')

def login(request):
    return render(request, 'blok/login.html')

# 标签学习
def get_parameter(request):
    content={}
    content["s"]="单个参数传递"
    content["lst"]=[1,2,3,4,5,6]
    content["key"]={"key1":"value1","key2":"value2"}
    content["say_hello"]= external_function.say_hello
    content["Dog"]= external_function.Dog
    return render(request, 'tag/parameters.html', content)

def page_caculator(request):
    if request.method=="GET":
        return render(request,"caculator.html",locals())

    elif request.method=="POST":
        x=int(request.POST.get("x"))
        y=int(request.POST.get("y"))
        op=request.POST.get("op")
        if op == "add":
            result=x+y
        elif op == "sub":
            result=x-y
        elif op == "mul":
            result=x*y
        elif op == "div":
            result=x/y
        print(locals())
        return render(request,"caculator.html",locals())

def page_for_tag(request):
    if request.method=="GET":
        lst=["北京","天津","重庆"]
        return render(request, "tag/fortag.html", locals())

def page_grep_tag(request):
    s="Hello World"
    origin="<i>Hello World</i>"
    number=2
    string="From river to sea Palestine will Be free"
    return render(request,"tag/filter.html",locals())

# 继承
# 主页
def page_parent(request):
    s="第11页 第12页 第13页"
    return render(request,"parent_child/base.html",locals())
# 新闻页
def page_news(request):
    return render(request,"parent_child/news.html")
# 音乐页
def page_music(request):
    return render(request,"parent_child/music.html")

def page_item(request,n):
    return HttpResponse("第"+str(n)+"页")